package com.guang.executor.executorpool;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.*;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @author lg
 * @Description  测试状态的转变
 * @date 2022/10/24 17:39
 */
public class ExecutorPoolTestTwo {
    private static Logger logger = LoggerFactory.getLogger(ExecutorPoolTestTwo.class);
    private static int CORE_THREAD_COUNT = Runtime.getRuntime().availableProcessors()+1;
    private static final String FIX_THREAD_NAME = "FixThreadPool_";
    // 不需要临时线程了，因为LinkedBlockingDeque默认长度是2的三十一次方-1
    private static final ExecutorService EXECUTOR_SERVICE = new ThreadPoolExecutor(CORE_THREAD_COUNT,
            CORE_THREAD_COUNT,
            5, TimeUnit.SECONDS,
            new LinkedBlockingDeque<>(),
            new ThreadFactory() {
                    AtomicInteger atomicInteger = new AtomicInteger(1);
                    // 自定义生产线程的工厂
                @Override
                public Thread newThread(Runnable r) {
                    return new Thread(r,FIX_THREAD_NAME+atomicInteger.getAndIncrement());
                }
            },new ThreadPoolExecutor.AbortPolicy()
    );

    public static void main(String[] args) throws InterruptedException {

        EXECUTOR_SERVICE.execute(()->{
            System.out.println("不断的消耗任务");
            logger.info("当前线程开始处理任务");
        });
        // 调用的时候小心一点！不可能在开启线程池之后就要将其进行销毁
        EXECUTOR_SERVICE.shutdown();

    }
}
